﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.auth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class SystemBranchDeptServiceImpl extends BaseBusinessService<SystemBranchDept,java.lang.Integer>  implements SystemBranchDeptService<SystemBranchDept,java.lang.Integer>{

	private SystemBranchDeptDao systemBranchDeptDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSystemBranchDeptDaoImpl(SystemBranchDeptDao dao) {
		this.systemBranchDeptDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.systemBranchDeptDao;
	}
	public void save(SystemBranchDept systemBranchDept){
		
		systemBranchDeptDao.save(systemBranchDept);
		systemBranchDeptDao.flush();
		List l=systemBranchDeptDao.getHibernateTemplate().find("from SystemBranchDept t where t.id=?",systemBranchDept.getParentId());		
		if(l!=null&&l.size()>0) {
			SystemBranchDept pSystemBranchDept=(SystemBranchDept)l.get(0);			
			systemBranchDept.setParentIds(StringHelper.formatNullValue(pSystemBranchDept.getParentIds(), "/")+systemBranchDept.getId()+"/");
		}else{
			systemBranchDept.setParentIds("/"+systemBranchDept.getId()+"/");
		}
		systemBranchDeptDao.update(systemBranchDept);
		systemBranchDeptDao.flush();
	}
	@Transactional(readOnly=true)
	public Page findPage(SystemBranchDeptQuery query) {
		return systemBranchDeptDao.findPage(query);
	}
	public void removeById(Integer id){
		List l=systemBranchDeptDao.getHibernateTemplate().find("from SystemBranchTitle t where t.deptId=?",id);
		if(l!=null&&l.size()>0) throw new RuntimeException("部门存在岗位,不能删除");
		l=systemBranchDeptDao.getHibernateTemplate().find("from SystemBranchDept t where t.parentId=?",id);
		if(l!=null&&l.size()>0) throw new RuntimeException("存在子部门,不能删除");
		systemBranchDeptDao.deleteById(id);
		systemBranchDeptDao.flush();
	}
	public List findSystemBranchDept(SystemBranchDeptQuery query) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		List values=new ArrayList();
		sql.append("from SystemBranchDept t where 1=1 ");
		if (ObjectUtils.isNotEmpty(query.getBranchNbr())){
			sql.append(" and t.branchNbr=? ");
			values.add(query.getBranchNbr());
		}
		if (ObjectUtils.isNotEmpty(query.getBranchCompanyId())){
			//sql.append(" and t.branchCompanyId=? ");
			//values.add(query.getBranchCompanyId());
			sql.append(" and t.branchCompanyId in (").append(query.getBranchCompanyId()).append(")");
		}
		sql.append(" order by t.seqNo desc");
		return systemBranchDeptDao.getHibernateTemplate().find(sql.toString(),values.toArray());
		
	}
	
}
